/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_osc.c
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the osc module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-23  |V2.0     |Wang Junxiong|New creat
 *****************************************************************************/

/******************************************************************************
 **                     QAC Warnings
 *****************************************************************************/
/* PRQA S 0436 EOF #8-bit MCU Features: Direct Write Registers. */
/* PRQA S 1291, 1820, 1821, 1822, 1823, 1851, 1853, 3715, 3757, 3760 EOF #Registers are essentially unsigned numbers. */
/* PRQA S 2754 EOF #Level 7 problem due to unsupported assembly instructions. */
/* PRQA S 3768, 3771, 4130, 4393, 4434, 4443, 4446, 4461, 4532 EOF #Registers are essentially unsigned numbers. */

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx_osc.h"
#include "DELAY.h"
/******************************************************************************
 *                      Macro Definitions
 *****************************************************************************/

/******************************************************************************
 *                      Constants
 *****************************************************************************/

/******************************************************************************
 *                      Type definitions
 *****************************************************************************/

/******************************************************************************
 *                      Variables
 *****************************************************************************/

/******************************************************************************
 *                      Function definitions
 *****************************************************************************/

/**
 * @brief Initialize the OSC, system clock must not fall below 4MHz.
 *
 * @param Source OSC system clock source selection.
 * @param Division OSC frequency coefficient.
 */
void Osc_Init(OSC_SourceType Source, Osc_IrcsType Division)
{
    uint16_t i = 0u;
    /* disable two-speed start, enable external crystal clock fault detection */
    IESO   = 0u;
    OSCCTL = 0x01u | (uint8_t)Source | (uint8_t)Division;
    /* Waiting for internal and external clock switching to complete */
    while ((SCF1 != SCS1) && (i < 870u))
    {
        Delay_2us_loop();
        i++;
    }
    IESO = 1u;
}

/**
 * @brief OSC Update Clock Source, system clock must not fall below 4MHz.
 *
 * @param Source OSC system clock source selection.
 */
void OSC_UpdateSource(OSC_SourceType Source)
{
    uint16_t i = 0u, reg_temp = OSCCTL & 0xf1u;
    /* disable two-speed start */
    IESO   = 0u;
    OSCCTL = reg_temp | (uint8_t)Source;
    /* Waiting for internal and external clock switching to complete */
    while ((SCF1 != SCS1) && (i < 870u))
    {
        Delay_2us_loop();
        i++;
    }
    IESO = 1u;
}

/* EOF */
